package persistance.dal;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import ucc.objects.OperationCaisseDTO;
import exception.DataAccesException;

public class OperationCaissePersisterImpl implements OperationCaissePersister {

	private PreparedStatement queryInsert;
	private PreparedStatement queryUpdate;
	private static final String INSERT = "insert into caisse.operations_caisse (horodatage, id_caisse_du_jour, id_produit, quantite, vente_animateur)"
			+ "values (?, ?, ?, ?, ?)";
	private static final String UPDATE = "update caisse.operations_caisse set "
			+ "horodatage=?, id_caisse_du_jour=?, id_produit=?, quantite=?, vente_animateur=? where id_operation_caisse=?";

	public OperationCaissePersisterImpl() {
		this.queryInsert = DalBackEndServices.INSTANCE.prepare(INSERT);
		this.queryUpdate = DalBackEndServices.INSTANCE.prepare(UPDATE);
	}

	public void insert(OperationCaisseDTO nouvelleOperationCaisse) {
		try {
			Timestamp time1 = new Timestamp(nouvelleOperationCaisse
					.getHorodatage().getTimeInMillis());
			queryInsert.setTimestamp(1, time1);
			queryInsert.setInt(2, nouvelleOperationCaisse.getIdCaisseDuJour());
			queryInsert.setInt(3, nouvelleOperationCaisse.getProduit().getId());
			queryInsert.setInt(4, nouvelleOperationCaisse.getQuantite());
			queryInsert.setBoolean(5,
					nouvelleOperationCaisse.isVenteAnimateur());
			queryInsert.execute();
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
			throw new DataAccesException(
					"SQL/JDBC Connection, Statement, executeQuery problem \n",
					sqlE);
		}
	}

	public void update(OperationCaisseDTO operaCaisseDTO) {
		try {
			Timestamp time1 = new Timestamp(operaCaisseDTO.getHorodatage()
					.getTimeInMillis());
			queryUpdate.setTimestamp(1, time1);
			queryUpdate.setInt(2, operaCaisseDTO.getIdCaisseDuJour());
			queryUpdate.setInt(3, operaCaisseDTO.getProduit().getId());
			queryUpdate.setInt(4, operaCaisseDTO.getQuantite());
			queryUpdate.setBoolean(5, operaCaisseDTO.isVenteAnimateur());
			queryUpdate.setObject(6, operaCaisseDTO.getId());
			queryUpdate.execute();
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();
			throw new DataAccesException(
					"SQL/JDBC Connection, Statement, executeQuery problem \n",
					sqlE);
		}
	}
}
